package net.projecteuler.programs.program0001;

import net.projecteuler.programs.TimeLogger;

/**
 * Multiples of 3 and 5
 *
 * If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
 * Find the sum of all the multiples of 3 or 5 below 1000.
 *
 * Created by zhaojjd on 2016/3/21.
 */
public class Program0001 {


    public long calculate(int maxnum){
        long t = 0;
        for (int i = 3; i < maxnum; i++) {
            if (i % 3 == 0 || i % 5 == 0) {
                t += i;
            }
        }
        return t;
    }

    public long calculate2(int maxnum){
        int max3 = (maxnum - 1) / 3;//+
        int max5 = (maxnum - 1) / 5;//+
        int max15 = (maxnum - 1) / 15;//-
        long t = (3 * (1 + max3) * max3 + 5 * (1 + max5) * max5 - 15 * (1 + max15) * max15) / 2;
        return t;
    }

    public static void main(String[] args) {
        Program0001 p = new Program0001();
        TimeLogger.tag();
        System.out.println(p.calculate(1000));
        TimeLogger.print();
        TimeLogger.tag();
        System.out.println(p.calculate2(1000));
        TimeLogger.print();
    }

}
